package com.pic.mgt.terminal.operation;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.file.io.FileOperation;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.utils.LogUtils;

public final class SimpleTerminalOperation implements TerminalOperation {
	private FileOperation fileOperation;

	private String haishinOutboxPathStr;

	@Override
	public File createTerminalFolder(String terminalId)
			throws TerminalOperationException {
		File terminalFolder = new File(haishinOutboxPathStr, terminalId);

		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this,
						"Terminal '{}' operation: creating folder '{}'.",
						terminalId, terminalFolder
								.getAbsolutePath());
			}
			// ********************** Log **********************
			
			if (!terminalFolder.exists()) {
				this.fileOperation.createFolder(terminalFolder);
			}

			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Terminal '{}' operation: creating folder '{}' completed.",
								terminalId,
								terminalFolder.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new TerminalOperationException("Terminal '"
						+ terminalId
						+ "' operation: creating folder '"
						+ terminalFolder.getAbsolutePath() + "' failed.", e);
			} catch (TerminalOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************

				throw e1;
			}
		}
		
		return terminalFolder;
	}

	@Override
	public File removeTerminalFolder(String terminalId)
			throws TerminalOperationException {
		File terminalFolder = new File(haishinOutboxPathStr, terminalId);

		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this,
						"Terminal '{}' operation: removing folder '{}'.",
						terminalId, terminalFolder
								.getAbsolutePath());
			}
			// ********************** Log **********************
			
			if (terminalFolder.exists()) {
				this.fileOperation.deleteFile(terminalFolder);
			}

			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Terminal '{}' operation: removing folder '{}' completed.",
								terminalId,
								terminalFolder.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new TerminalOperationException("Terminal '"
						+ terminalId
						+ "' operation: removing folder '"
						+ terminalFolder.getAbsolutePath() + "' failed.", e);
			} catch (TerminalOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************

				throw e1;
			}
		}
		
		return terminalFolder;
	}

	// --- getter and setter ---

	public void setFileOperation(FileOperation fileOperation) {
		this.fileOperation = fileOperation;
	}

	public void setHaishinOutboxPathStr(String haishinOutboxPathStr) {
		this.haishinOutboxPathStr = haishinOutboxPathStr;
	}

}
